clear

set graphics on

*Generate output tables

local nuts 2

use "$BvD_output/BvD_results_loop`nuts'.dta" if co!="BE" & co!="FI" & co!="IE", replace
gen Country="Austria" if co=="AT"
replace Country="Hungary" if co=="HU"
replace Country="United States" if co=="US"
replace Country="Czech Republic" if co=="CZ"
replace Country="Denmark" if co=="DK"
replace Country="Germany" if co=="DE"
replace Country="United Kingdom" if co=="GB"
replace Country="Italy" if co=="IT"
replace Country="Norway" if co=="NO"
replace Country="Poland" if co=="PL"
replace Country="Romania" if co=="RO"
replace Country="Sweden" if co=="SE"
replace Country="Portugal" if co=="PT"
replace Country="France" if co=="FR"
replace Country="Spain" if co=="ES"
replace Country="Bulgaria" if co=="BG"

foreach svar in xi tau{
bys tfpvar Country (cov_`svar'b): gen cov_ub_`svar'=cov_`svar'b[975]
bys tfpvar Country (cov_`svar'b): gen cov_lb_`svar'=cov_`svar'b[25]
bys tfpvar Country (cov_`svar'b): egen cov_mean_`svar'=mean(cov_`svar'b)
}

keep cov_ub* cov_lb* cov_mean* Country tfpvar co
duplicates drop

tempfile cov
save `cov'

levelsof Country, local(Countrylist)
levelsof co, local(colist)

tempfile perm
foreach co in `colist'{
use "$BvD_output/permute_out_stats_`co'`nuts'.dta", clear
foreach svar in xi tau{
foreach ii of numlist 1(1)3{
	sort var_`svar'_`ii'
	tempvar pval_`var'
	gen `pval_`var''			=_n
	sort iter
	gen perm_pval_`svar'_`ii'		=(_N-`pval_`var''[1]+1)/1000
	}
	}
ren var* perm_var*
reshape long perm_var_xi perm_var_tau perm_pval_xi perm_pval_tau, i(iter) j(tfpvar) string
drop if iter==0
foreach svar in xi tau{
bys tfpvar: egen perm_mean_`svar'= mean(perm_var_`svar')
bys tfpvar: egen perm_sd_`svar'= sd(perm_var_`svar')
}
replace tfpvar="w1_lvapw" if tfpvar=="_1"
**A robustness check uses FIAS (fixed assets) as capital measure for TFP; see permute do file. Here, we use a filler for now in its place. Change code to conduct this robustness check:
replace tfpvar="w1_log_TFPr_fias" if tfpvar=="_2"
replace tfpvar="w1_log_TFPr_tfas" if tfpvar=="_3"
keep *_mean* *_sd* *pval* co tfpvar
duplicates drop
cap append using `perm'
save `perm', replace
}
merge 1:1 tfpvar co using `cov', nogen
merge 1:1 tfpvar co using "$BvD_output/BvD_results_`nuts'.dta", nogen keepusing(tfpvar co var_tau var_xi count_* *_p9010)
cap drop *prelim
drop if tfpvar=="w1_log_TFPr_fias"
drop co 
sort tfpvar Country
order tfpvar Country var_xi xi_p9010 perm_mean_xi perm_sd_xi perm_pval_xi cov_mean_xi cov_ub_xi cov_lb_xi var_tau tau_p9010 perm_mean_tau perm_sd_tau perm_pval_tau cov_mean_tau cov_ub_tau cov_lb_tau
gen _varname = subinstr(Country, " ", "_", .)
tempfile main
save `main'
drop if tfpvar=="w1_lvapw"
drop tfpvar Country
xpose , clear varname
rename _varname Stat
order Stat, alphabet

foreach var of varlist _all{
	if("`var'"!="Stat"){
		rename `var' `var'_num
		replace `var'_num=`var'_num*100 if Stat!="perm_pval_xi" & Stat!="perm_pval_tau" & Stat!="count_firms" & Stat!="count_xi" & Stat!="count_tau"  
		gen `var'=string(`var'_num)
		 replace `var'=string(`var'_num,"%9.2f") if Stat!="perm_pval_xi" & Stat!="perm_pval_tau" & Stat!="count_firms" & Stat!="count_xi" & Stat!="count_tau"  
		drop `var'_num
		 }
	}
	
replace Stat="Empirical: \\  \hspace{5mm}Raw $\Var(\widehat{\xi_l})$" if Stat=="var_xi"
replace Stat="Empirical: \\  \hspace{5mm}Raw $\Var(\widehat{\tau_{l,i}})$" if Stat=="var_tau"
replace Stat="\hspace{5mm}90/10 Percentile Gap " if Stat=="xi_p9010" | Stat=="tau_p9010"
replace Stat="Permutations:\\  \hspace{5mm}Mean $\Var(\widehat{\xi_l})$" if Stat=="perm_mean_xi"
replace Stat="\hspace{5mm}Standard Deviation" if Stat=="perm_sd_xi" | Stat=="perm_sd_tau"
replace Stat="\hspace{5mm}p-value" if Stat=="perm_pval_xi" | Stat=="perm_pval_tau"
replace Stat="Bias-Corrected:\\ \hspace{5mm}Mean $\Cov(\widehat{\xi^A_l},\widehat{\xi^B_l})=\Var(\xi_l)$" if Stat=="cov_mean_xi"
replace Stat="\hspace{5mm} 97.5\% Value" if Stat=="cov_ub_xi" | Stat=="cov_ub_tau" 
replace Stat="\hspace{5mm} 2.5\% Value" if Stat=="cov_lb_xi" | Stat=="cov_lb_tau"
replace Stat="Permutations:\\  \hspace{5mm}Mean $\Var(\widehat{\tau_{l,i}})$" if Stat=="perm_mean_tau"
replace Stat="Bias-Corrected:\\ \hspace{5mm}Mean $\Cov(\widehat{\tau^A_{l,i}},\widehat{\tau^B_{l,i}})=\Var(\tau_{l,i})$" if Stat=="cov_mean_tau"
replace Stat="$ N $ Locations" if Stat=="count_xi" 
replace Stat="$ N $ Industry-Locations" if Stat=="count_tau" 
replace Stat="$ N $ Firms" if Stat=="count_firms" 

dataout, save("$BvD_exhibits/Table_A2_TFAS") tex replace

use `main', replace

drop if tfpvar=="w1_log_TFPr_tfas"
drop tfpvar Country
xpose , clear varname
rename _varname Stat
order Stat, alphabet

foreach var of varlist _all{
	if("`var'"!="Stat"){
		rename `var' `var'_num
		replace `var'_num=`var'_num*100 if Stat!="perm_pval_xi" & Stat!="perm_pval_tau" & Stat!="count_firms" & Stat!="count_xi" & Stat!="count_tau"  
		gen `var'=string(`var'_num)
		 replace `var'=string(`var'_num,"%9.2f") if Stat!="perm_pval_xi" & Stat!="perm_pval_tau" & Stat!="count_firms" & Stat!="count_xi" & Stat!="count_tau"  
		drop `var'_num
		 }
	}
	
replace Stat="Empirical: \\  \hspace{5mm}Raw $\Var(\widehat{\xi_l})$" if Stat=="var_xi"
replace Stat="Empirical: \\  \hspace{5mm}Raw $\Var(\widehat{\tau_{l,i}})$" if Stat=="var_tau"
replace Stat="\hspace{5mm}90/10 Percentile Gap " if Stat=="xi_p9010" | Stat=="tau_p9010"
replace Stat="Permutations:\\  \hspace{5mm}Mean $\Var(\widehat{\xi_l})$" if Stat=="perm_mean_xi"
replace Stat="\hspace{5mm}Standard Deviation" if Stat=="perm_sd_xi" | Stat=="perm_sd_tau"
replace Stat="\hspace{5mm}p-value" if Stat=="perm_pval_xi" | Stat=="perm_pval_tau"
replace Stat="Bias-Corrected:\\ \hspace{5mm}Mean $\Cov(\widehat{\xi^A_l},\widehat{\xi^B_l})=\Var(\xi_l)$" if Stat=="cov_mean_xi"
replace Stat="\hspace{5mm} 97.5\% Value" if Stat=="cov_ub_xi" | Stat=="cov_ub_tau" 
replace Stat="\hspace{5mm} 2.5\% Value" if Stat=="cov_lb_xi" | Stat=="cov_lb_tau"
replace Stat="Permutations:\\  \hspace{5mm}Mean $\Var(\widehat{\tau_{l,i}})$" if Stat=="perm_mean_tau"
replace Stat="Bias-Corrected:\\ \hspace{5mm}Mean $\Cov(\widehat{\tau^A_{l,i}},\widehat{\tau^B_{l,i}})=\Var(\tau_{l,i})$" if Stat=="cov_mean_tau"
replace Stat="$ N $ Locations" if Stat=="count_xi" 
replace Stat="$ N $ Industry-Locations" if Stat=="count_tau" 
replace Stat="$ N $ Firms" if Stat=="count_firms" 

dataout, save("$BvD_exhibits/Table_A3_lvapw") tex replace
